/**
 * Copyright(c) Hainan eKing Technology co., LTD
 *
 * @date 2013年9月11日
 * 	
 * Original Author: 郑兴(zhengxing)
 *
 * ChangeLog
 *
 */
package com.hnas.sys.dao;

import java.util.List;

import javax.persistence.Query;

import org.springframework.stereotype.Component;

import com.hnas.core.dao.BaseDaoImpl;
import com.hnas.core.util.StringUtil;
import com.hnas.sys.orm.User;

/**
 * @author 郑兴(zhengxing)
 *
 */
@Component("userDAO")
public class UserDAOImpl extends BaseDaoImpl<User> implements UserDAO {
	public UserDAOImpl() {
		super(User.class);
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.hna.adt.dao.user.UserDAO#findByUserName(java.lang.String)
	 */
	public User findByUserName(String userName) {
		Query query = entityManager.createQuery("From User u left join fetch u.authoritys where u.account = ? ");
		query.setParameter(1, userName);
//		query.setParameter(2, UserType.MANAGER);
		List<User> ls = query.getResultList();
		if (ls.size()>0)
			return ls.get(0);
//		User user = (User) query.getSingleResult();
		return null;
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.hna.adt.dao.user.UserDAO#loadAll()
	 */
	public List<User> loadAll() {
		Query query = entityManager.createQuery("From User");
		return query.getResultList();
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.hna.adt.dao.user.UserDAO#getUserByAccount()
	 */
	public User getUserByAccount(String account) {
		// TODO Auto-generated method stub
		String query = "from User t where t.account=?1 and statusStr='VALID'";
		List<User> ls = this.findByQuery(query, account);
		if (ls.size() > 0)
			return ls.get(0);
		return null;
	}

	/*
	 * (non-Javadoc)
	 * @see com.hna.adt.dao.user.UserDAO#getUserWithRoleAdmin()
	 */
//	public List<User> getUserWithRoleAdmin() {
//		Query query = entityManager.createQuery("FROM User u LEFT JOIN FETCH u.authorities a WHERE a.code = ?1 ");
//		query.setParameter(1, AuthorityCode.ROLE_AGREEMENT_ADMIN);
//		return query.getResultList();
//	}

	
	public List<User> getUserWithRole(String account, String roleCode) {
		String sql = "FROM User u LEFT JOIN FETCH u.authorities a WHERE a.code = ?1 ";
		if (StringUtil.isNotEmpty(account)) {
			sql = sql + " AND u.account=?2 ";
		}
		Query query = entityManager.createQuery(sql);
		query.setParameter(1, roleCode);
		if (StringUtil.isNotEmpty(account)) {
			query.setParameter(2, account);
		}
		return query.getResultList();
	}
	public List<User> getUserByAgencyid(long Agencyid) {
		String sql = "FROM User u  WHERE u.agency.id = ?1 and u.statusStr='VALID' ";
		Query query = entityManager.createQuery(sql);
		query.setParameter(1, Agencyid);
		return query.getResultList();
	}
	public List<User> getUsersWithRole(String roleName) {
		// TODO Auto-generated method stub
		String hql = "select t from User t join t.authoritys b where b.name=?1";
		return this.findByQuery(hql, roleName);
	}

//	@Override
//	public List<User> getUserWithManagerRole(String account) {
//		String sql = "SELECT DISTINCT(u) FROM User u LEFT JOIN FETCH u.authorities a WHERE (a.code = ?1 OR a.code = ?2 OR a.code = ?3 )";
//		if (StringUtil.isNotEmpty(account)) {
//			sql = sql + " AND u.account=?4 ";
//		}
//		Query query = entityManager.createQuery(sql);
//		query.setParameter(1, AuthorityCode.ROLE_ADMIN);
//		query.setParameter(2, AuthorityCode.ROLE_AGREEMENT_ADMIN);
//		query.setParameter(3, AuthorityCode.ROLE_SYS_MANAGER);
//		if (StringUtil.isNotEmpty(account)) {
//			query.setParameter(4, account);
//		}
//		return query.getResultList();
//	}
	
}
